/* Reken machine voor op de PDA
 * Werkt op een PPC met WM5.0, WM6.0 en WM6.1
 * met Mysaifu JVM
 */
package andere_dingen;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class PDARekenDing {

  JFrame frame;
  JTextArea resultaat;
  JTextField textA, textB, textC;

  public PDARekenDing() {
    JOptionPane.showMessageDialog(null, "Berekeningen\n die maar 1\nnodig hebben werken \nalleen in het bovenste \nveld. en voor 2 alleen\nde 2 bovenste etc.");
    initComponents();
    frame.repaint();
  }

  public boolean isPriem(double getal) {
    int g = (int) getal;
    if (g==1){
      return false;
    }
    for (int divisor = 2; divisor <= g / 2; divisor++) {
      if (g % divisor == 0) {
        //is geen priemgetal
        return false;
      }
    }
    return true;
  }

  public void opDeKnopGeklikt() {
    double a = 0, b = 0, c = 0;
    boolean aOk, bOk, cOk;

    //A
    try {
      a = Double.parseDouble(textA.getText());
      aOk = true;
    } catch (Exception e) {
      aOk = false;
    }
    //B
    try {
      b = Double.parseDouble(textB.getText());
      bOk = true;
    } catch (Exception e) {
      bOk = false;
    }
    //C
    try {
      c = Double.parseDouble(textC.getText());
      cOk = true;
    } catch (Exception e) {
      cOk = false;
    }
    //We hebben nu getallen.
    //Als het goed is.
    int aantalGetallen = 0;
    if (aOk == true) {
      aantalGetallen += 1;
    }
    if (bOk == true) {
      aantalGetallen += 1;
    }
    if (cOk == true) {
      aantalGetallen += 1;
    }
    //We weten nu ook hoeveel getallen we hebben.
    String deText = "Resultaten: (" + aantalGetallen + ")\n";
    if (aantalGetallen == 0) {
      deText += "Er zijn geen getallen ingevuld";
    }

    //Alle berekeningen doen die je met 1 getal kan doen
    //welk(e) getal(len) is/zijn er ?
    if (aOk) {
      deText += "1e getal:\n";
      //Priemgetal
      if (isPriem(a)) {
        deText += (int) a + " is WEL Priemgetal\n";
      } else {
        deText += (int) a + " is GEEN Priemgetal\n";
      }
      //km/h -> m/s
      deText += a + "km/h = " + (a / 3.6) + "m/s\n";
      //m/s -> km/h
      deText += a + "m/s = " + (a * 3.6) + "km/h\n";
      //inch -> cm
      deText += a + "cm = " + (a / 2.54) + "inch\n";
      deText += a + "inch = " + (a * 2.54) + "cm\n";
      deText += "============\n";
    }
    if (aOk && bOk) {
      deText += "1e en 2e getal:\n";
      //Alle berekeningen doen die je met 2 getallen kan doen
      deText += a + "+" + b + "=" + (a + b) + "\n";
      deText += a + "-" + b + "=" + (a - b) + "\n";
      deText += a + "*" + b + "=" + (a * b) + "\n";
      deText += a + "/" + b + "=" + (a / b) + "\n";
      deText += a + "%" + b + "=" + (a % b) + "\n";
      deText += a + "^" + b + "=" + Math.pow(a, b) + "\n";
      //GGD
      double gcd = 1;
      double k = 2;
      while (k <= a && k <= b) {
        if (a % k == 0 && b % k == 0) {
          gcd = k;
        }
        k++;
      }
      deText += a + "GGD" + b + "=" + gcd+ "\n";
      //kgv
      double kgv = ((a * b) / (gcd));
      deText += a + "KGV" + b + "=" + kgv + "\n";
      deText += "============\n";
    }
    if (aOk && bOk && cOk) {
      //Alle berekeningen doen die je met 3 getallen kan doen
      //De ABC
      double laag  = (((-b)-(Math.sqrt(Math.pow(b,2)-4*a*c)))/(2*a));
      double hoog  = (((-b)+(Math.sqrt(Math.pow(b,2)-4*a*c)))/(2*a));
      deText += "abc-: "+ laag+ "\n";
      deText += "abc+:" + hoog + "\n";
    }
    //De text Zetten
    resultaat.setText(deText);
  }

  public void initComponents() {
    frame = new JFrame();
    frame.setLayout(null);
    frame.setSize(240, 280); //dan past het op het PDA scherm
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setTitle("Pda Reken Ding");
    //Label
    JLabel title = new JLabel();
    title.setText("Pda Reken Ding");
    title.setBounds(5, 5, 220, 20);
    frame.add(title);
    //Getal A
    textA = new JTextField();
    textA.setBounds(5, 25, 220, 20);
    frame.add(textA);
    //Getal B
    textB = new JTextField();
    textB.setBounds(5, 55, 220, 20);
    frame.add(textB);
    //Getal C
    textC = new JTextField();
    textC.setBounds(5, 85, 220, 20);
    frame.add(textC);
    //Knop
    JButton knop = new JButton("Bereken");
    knop.setBounds(5, 115, 220, 20);
    //knop.setText("Bereken");
    knop.addActionListener(new ButtonListener());
    frame.add(knop);
    //Resultaat
    resultaat = new JTextArea();
    JScrollPane scrollResultaat = new JScrollPane(resultaat);
    resultaat.setText("Hier komen de antwoorden...");
    scrollResultaat.setBounds(5, 145, 220, 280 - 180);
    frame.add(scrollResultaat);
  }

  public static void main(String[] args) {
    new PDARekenDing();
  }

  class ButtonListener implements ActionListener {

    ButtonListener() {
    }

    public void actionPerformed(ActionEvent e) {
      //Op De Knop Geklikt
      opDeKnopGeklikt();
    }
  }
}

